home *** CD-ROM | disk | FTP | other *** search
/ Technotools / Technotools (Chestnut CD-ROM)(1993).ISO / lang_oth / forchek1 / symtab.c < prev    next >
C/C++ Source or Header  |  1991-11-05  |  56KB  |  2,127 lines

  1. /* symtab.c:
  2.  
  3. Contains formerly separate modules:
  4.    I. Symtab: symbol table maintenance routines.
  5.   II. Hash:  hash table functions: hash(), kwd_hash(), rehash()
  6.  III. Intrins: handles recognition & data typing of intrinsic functions.
  7.  
  8.  
  9.     Copyright (C) 1991 by Robert K. Moniot.
  10.     This program is free software.  Permission is granted to
  11.     modify it and/or redistribute it, retaining this notice.
  12.     No guarantees accompany this software.
  13.  
  14.  
  15. */
  16.  
  17. /*
  18.   I. Symtab
  19.  
  20.  
  21.   Symbol table routines for Fortran program checker.
  22.  
  23.    Shared functions defined:
  24.  
  25.  
  26.     call_func(id,arg)  Handles function invocations.
  27.     call_subr(id,arg)  Handles CALL statements.
  28.     declare_type(id,datatype) Handles TYPE statements.
  29.     def_arg_name(id)  Handles func/subr argument lists.
  30.     def_array_dim(id,arg) Handles dimensioning declarations.
  31.     def_com_block(id)  Handles common blocks and SAVE stmts.
  32.     def_com_variable(id)  Handles common block lists.
  33.        int def_curr_module(id)  Identifies symbol as current module.
  34.          def_equiv_name(id)  Initializes equivalence list items.
  35.     def_ext_name(id)  Handles external lists.
  36.     def_function(datatype,id,args)
  37.       Installs function name in global table.
  38.     def_intrins_name(id)  Handles intrinsic lists.
  39.     def_parameter(id,value) Handles parameter_defn_item
  40.     def_stmt_function(id) Declares a statement function.
  41.     do_ASSIGN(id)  Handles ASSIGN stmts.
  42.     do_assigned_GOTO(id)  Handles assigned GOTO.
  43.     do_ENTRY(id,args,hashno) Processes ENTRY statement.
  44.     do_RETURN(hashno,keyword) Processes RETURN statement.
  45.     equivalence(id1,id2)  equivalences two variables
  46.        int get_type(symt)  Finds out data type of symbol, or uses implicit
  47.      typing to establish its type.
  48.  unsigned hash_lookup(s)  Looks up identifier in hashtable.
  49.     init_globals()  Initializes global symbol info.
  50.     init_symtab()  Clears local symbol table & removes locals
  51.      from stringspace. Also restores default
  52.      implicit data typing.
  53.  symtab* install_global(t,datatype,storage_class) Installs indentifier in
  54.     global symbol table.
  55.  symtab* install_local(t,datatype,storage_class) Installs indentifier in
  56.     local symbol table.
  57. ArgListHeader* make_arg_array(t) Converts list of tokens into list of
  58.      type-flag pairs.
  59. ArgListHeader* make_dummy_arg_array(t) Converts list of tokens into list of
  60.      type-flag pairs.
  61. ArgListHeader* make_arrayless_alist() Sets up argument list header for
  62.     EXTERNAL decl or subprog as actual arg.
  63. ComListHeader* make_com_array(t) Converts list of common block tokens into
  64.      list of dimen_info-type pairs.
  65.     process_lists()  Places pointer to linked list of arrays in
  66.      global symbol table
  67.     ref_array(id,subscrs) Handles array references
  68.     ref_variable(id)  Handles accessing variable name.
  69.     set_implicit_type(type,c1,c2) Processes IMPLICIT statement.
  70.     stmt_function_stmt(id) Finishes processing stmt func defn.
  71.     char * token_name(t)  Returns ptr to token's symbol's name.
  72.     use_actual_arg(id)  Handles using a variable as actual arg.
  73.     use_io_keyword(id_keywd,id_val,class) Handles i/o control specifier.
  74.     use_lvalue(id)  Handles assignment to a variable.
  75.     use_parameter(id)  Handles data_constant_value &
  76.      data_repeat_factor.
  77.     use_variable(id)  Sets used-flag for a variable used in expr.
  78.  
  79. */
  80.  
  81. /*  private functions defined:
  82.  arg_count(t)  Counts the number of arguments in a token list.
  83.  call_external(symt,id,arg) places token list of args into local symtab
  84.  check_intrins_args(arg, defn) Checks call seq of intrinsic functions
  85.  check_stmt_function_args(symt,id,arg)  ditto for statement functions
  86.  find_intrinsic()  Looks up intrinsic functions in table
  87.  find_io_keyword()  Looks up i/o control spec keywords
  88.  reverse_tokenlist(t)  Reverses a linked list of tokens
  89.  make_TL_head();  Initializes a tokenlist header
  90. */
  91.  
  92. #include <stdio.h>
  93. #include <string.h>
  94. #include <ctype.h>
  95. #define SYMTAB
  96. #include "forchek.h"
  97. #include "symtab.h"
  98. #include "tokdefs.h"
  99. #ifdef __STDC__
  100. #include <stdlib.h>
  101. #else
  102. char *calloc();
  103. void exit();
  104. #endif
  105.  
  106.  
  107. PRIVATE
  108. unsigned arg_count();
  109.  
  110. PRIVATE void
  111. call_external(),
  112. check_intrins_args(),
  113. check_stmt_function_args();
  114.  
  115. PRIVATE int
  116. find_io_keyword();
  117.  
  118. PRIVATE Token *
  119. reverse_tokenlist();
  120.  
  121. PRIVATE TokenListHeader * /* Initializes a tokenlist header */
  122. make_TL_head();
  123.  
  124. PRIVATE
  125. ArgListHeader *make_dummy_arg_array(),*make_arg_array(),
  126.  *make_arrayless_alist();
  127.  
  128. PRIVATE
  129. ComListHeader *make_com_array();
  130.  
  131. PRIVATE
  132. IntrinsInfo *find_intrinsic();
  133.  
  134. PRIVATE unsigned
  135. arg_count(t)            /* Counts the number of arguments in a token list */
  136.  Token *t;
  137. {
  138.  unsigned count;
  139.  count = 0;
  140.  while(t != NULL){
  141.   count++;
  142.   t = t->next_token;
  143.  }
  144.  return(count);
  145. }
  146.  
  147.    /* This routine handles the saving of arg lists which
  148.       is done by call_func and call_subr */
  149. PRIVATE void
  150. call_external(symt,id,arg)
  151.  symtab *symt;
  152.  Token *id,*arg;
  153. {
  154.         TokenListHeader *TH_ptr;
  155.  
  156.   /* Insert the new list onto linked list of token lists */
  157.        TH_ptr= make_TL_head(id);
  158.  
  159.  TH_ptr->tokenlist = (arg == NULL ? NULL: arg->next_token);
  160.  TH_ptr->next = symt->info.toklist;
  161.  symt->info.toklist = TH_ptr;
  162. } /*call_external*/
  163.  
  164. void
  165. call_func(id,arg) /* Process function invocation */
  166.  Token *id, *arg;
  167. {
  168.  int t, h=id->value.integer;
  169.  symtab *symt,*gsymt;
  170.  IntrinsInfo *defn;
  171.  
  172.  if( (symt = (hashtab[h].loc_symtab)) == NULL){
  173.     symt = install_local(h,type_UNDECL,class_SUBPROGRAM);
  174.            symt->info.toklist = NULL;
  175.  }
  176.  
  177.  t = datatype_of(symt->type);
  178.   /* Symbol seen before: check it & change class */
  179.  
  180.  if(storage_class_of(symt->type) == class_VAR) {
  181.      symt->type = type_byte(class_SUBPROGRAM,t);
  182.      symt->info.toklist = NULL;
  183.    }
  184.  
  185.  
  186.   /* See if intrinsic.  If so, set flag, save info */
  187.     if(!symt->external && !symt->intrinsic
  188.   && (defn = find_intrinsic(symt->name)) != NULL) {
  189.    /* First encounter with intrinsic fcn: store info */
  190.   symt->intrinsic = TRUE;
  191.   symt->info.intrins_info = defn;
  192.     }
  193.  
  194.  
  195.   /* If intrinsic, do checking now.  Otherwise, save arg list
  196.      to be checked later. */
  197.  
  198.     if(symt->intrinsic) {
  199.    /* It is intrinsic: check it */
  200.  check_intrins_args(arg,symt->info.intrins_info);
  201.     }
  202.     else {  /* It is not intrinsic: install in global table */
  203.       switch(storage_class_of(symt->type)) {
  204.  case class_SUBPROGRAM:
  205.    symt->external = TRUE;
  206.    if((!symt->argument) && (gsymt=(hashtab[h].glob_symtab)) == NULL) {
  207.   gsymt = install_global(h,type_UNDECL,class_SUBPROGRAM);
  208.   gsymt->info.arglist = NULL;
  209.    }
  210.    /* store arg list in local table */
  211.    call_external(symt,id,arg);
  212.    break;
  213.  case class_STMT_FUNCTION:
  214.    symt->external = TRUE;
  215.    check_stmt_function_args(symt,id,arg);
  216.    break;
  217.       }
  218.     }
  219.  
  220.     symt->used_flag = TRUE;
  221.     symt->invoked_as_func = TRUE;
  222.  
  223. } /*call_func*/
  224.  
  225.  
  226. void
  227. call_subr(id,arg) /* Process call statements */
  228.  Token *id, *arg;
  229. {
  230.  int t, h=id->value.integer;
  231.  symtab *symt,*gsymt;
  232.  
  233.  if( (symt = (hashtab[h].loc_symtab)) == NULL){
  234.     symt = install_local(h,type_SUBROUTINE,class_SUBPROGRAM);
  235.        symt->info.toklist = NULL;
  236.  }
  237.  
  238.  
  239.  t=datatype_of(symt->type);
  240.   /* Symbol seen before: check it & change class */
  241.  
  242.  if(t == type_UNDECL) {
  243.   t = type_SUBROUTINE;
  244.   symt->info.toklist = NULL;
  245.  }
  246.  symt->type = type_byte(class_SUBPROGRAM,t);
  247.  
  248.  /* Assume CALL cannot refer to intrinsic, so don't look to
  249.     see if it is in intrinsic list.
  250.     But if declared intrinsic, then accept it as such and
  251.     do checking now.  Otherwise, save arg list
  252.     to be checked later. */
  253.  
  254.     if(symt->intrinsic) {
  255.    /* It is intrinsic: check it */
  256.  check_intrins_args(arg,symt->info.intrins_info);
  257.     }
  258.     else {  /* It is not intrinsic: install in global table */
  259.  symt->external = TRUE;
  260.  if((!symt->argument) && (gsymt=(hashtab[h].glob_symtab)) == NULL) {
  261.   gsymt = install_global(h,type_UNDECL,class_SUBPROGRAM);
  262.   gsymt->info.arglist = NULL;
  263.  }
  264.    /* store arg list in local table */
  265.  call_external(symt,id,arg);
  266.     }
  267.  
  268.  symt->used_flag = TRUE;
  269.  
  270. }/*call_subr*/
  271.  
  272.  
  273.   /* check out consistency of intrinsic argument list */
  274. PRIVATE
  275. void
  276. check_intrins_args(arg, defn)
  277.  Token *arg;
  278.  IntrinsInfo *defn;
  279. {
  280.  int i;
  281.  unsigned args_given = arg_count(arg->next_token);
  282.  int type,firsttype;
  283.  int numargs,argtype;
  284.  Token *t;
  285.  
  286.  numargs = defn->num_args;
  287.  argtype = defn->arg_type;
  288.  
  289.  
  290.    /* positive numargs: must agree */
  291.  if( (numargs > 0 && (args_given != numargs))
  292.    /* numargs == -1: 1 or 2 */
  293.   || (numargs == -1 && (args_given != 1 && args_given != 2))
  294.    /* numargs == -2: 2 or more */
  295.   || (numargs == -2 && (args_given < 2)) ){
  296.   syntax_error(arg->line_num,arg->col_num,
  297.     "intrinsic function used with wrong number of arguments: ");
  298.   msg_tail(defn->name);
  299.  }
  300.  if(arg == NULL) return;
  301.  
  302.  t = arg->next_token;
  303.  for(i=0; i<args_given; i++) {
  304.      type = datatype_of(t->class);
  305.  
  306.      if(i == 0)
  307.   firsttype = type;
  308.  
  309.      if(!( (1<<type) & argtype )) {
  310.   syntax_error(t->line_num,t->col_num,
  311.   "illegal argument data type for intrinsic function");
  312.      }
  313.  
  314.      if(firsttype != type) {
  315.   syntax_error(t->line_num,t->col_num,
  316.   "intrinsic function argument data types differ");
  317.      }
  318.      t = t->next_token;
  319.  }
  320. }/* check_intrins_args */
  321.  
  322.  
  323. PRIVATE
  324. void
  325. check_stmt_function_args(symt,id,arg)
  326.  symtab *symt;
  327.  Token *id,*arg;
  328. {
  329.  unsigned n1,n2,n;
  330.  int i;
  331.  Token *t1,*t2;
  332.  
  333.  t1 = symt->info.toklist->tokenlist;
  334.  t2 = reverse_tokenlist( (arg==NULL? NULL : arg->next_token) );
  335.  
  336.  n1 = arg_count(t1);
  337.  n2 = arg_count(t2);
  338.  
  339.  if(n1 != n2) {
  340.      syntax_error(id->line_num,id->col_num,
  341.   "function invoked with incorrect number of arguments");
  342.  }
  343.  
  344.  n = (n1 < n2? n1: n2);
  345.  for(i=0; i<n; i++) {
  346.  
  347.      if( t1->class != t2->class) {
  348.   syntax_error(t2->line_num,t2->col_num,
  349.     "function argument is of incorrect datatype");
  350.      }
  351.      t1 = t1->next_token;
  352.      t2 = t2->next_token;
  353.  }
  354. }
  355.  
  356.  
  357. void
  358. declare_type(id,datatype)
  359.  Token *id;
  360.  int datatype;
  361. {
  362.  int h=id->value.integer;
  363.  symtab *symt;
  364.  
  365.  if( (symt=hashtab[h].loc_symtab) == NULL) {
  366.     symt = install_local(h,datatype,class_VAR);
  367.  }
  368.  else {           /* Symbol has been seen before: check it */
  369.  
  370.    /* Intrinsic: see if type is consistent */
  371.    if( symt->intrinsic ) {
  372.      IntrinsInfo *defn = symt->info.intrins_info;
  373.      int rettype = defn->result_type,
  374.   argtype = defn->arg_type;
  375.    /* N.B. this test catches many but not all errors */
  376.      if( (rettype != type_GENERIC && datatype != rettype)
  377.       || (rettype == type_GENERIC && !((1<<datatype) & argtype)) ){
  378.       warning(id->line_num,id->col_num,
  379.     "Declared type ");
  380.       msg_tail(type_name[datatype]);
  381.       msg_tail(" is invalid for intrinsic function: ");
  382.       msg_tail(symt->name);
  383.        }
  384.    }
  385.  
  386.    if(datatype_of(symt->type) != type_UNDECL) {
  387.        syntax_error(id->line_num,id->col_num,
  388.   "Symbol redeclared: ");
  389.     msg_tail(symt->name);
  390.    }
  391.    else {
  392.    /* Now give it the declared type */
  393.        symt->type = type_byte(storage_class_of(symt->type),datatype);
  394.    }
  395.  }
  396. }/*declare_type*/
  397.  
  398.  
  399. void
  400. def_arg_name(id)  /* Process items in argument list */
  401.  
  402.  Token *id;
  403. {
  404.  int h=id->value.integer;
  405.  symtab *symt;
  406.  
  407.  if( (symt=hashtab[h].loc_symtab) == NULL) {
  408.     symt = install_local(h,type_UNDECL,class_VAR);
  409.  }
  410.  else {           /* Symbol has been seen before: check it */
  411.  
  412.  }
  413.  symt->argument = TRUE;
  414. }/*def_arg_name*/
  415.  
  416.  
  417. void
  418. def_array_dim(id,arg) /* Process dimension lists */
  419.  Token *id,*arg;      /* arg previously defined as int */
  420. {
  421.  int h=id->value.integer;
  422.  symtab *symt;
  423.  
  424.  
  425.  if( (symt=hashtab[h].loc_symtab) == NULL) {
  426.     symt = install_local(h,type_UNDECL,class_VAR);
  427.  }
  428.  else {           /* Symbol has been seen before: check it */
  429.     if(storage_class_of(symt->type) != class_VAR) {
  430.        syntax_error(id->line_num,id->col_num,
  431.   "Entity cannot be dimensioned: ");
  432.   msg_tail(symt->name);
  433.        return;
  434.     }
  435.  }
  436.  symt->array_var = TRUE;
  437.  if(!equivalence_flag){      /* some checking should be done here */
  438.     if(symt->info.array_dim != 0)
  439.        syntax_error(id->line_num,id->col_num,
  440.   "Array redimensioned");
  441.     else
  442.        symt->info.array_dim = array_dim_info(arg->class,arg->subclass);
  443.  }
  444. }/*def_array_dim*/
  445.  
  446.  
  447. void
  448. def_com_block(id,comlist) /* Process common blocks and save_stmt */
  449.  Token *id, *comlist;
  450.  
  451. {
  452.  int h=id->value.integer;
  453.  symtab *symt,*gsymt;
  454.     TokenListHeader *TH_ptr;
  455.  
  456.   /* Install name in global symbol table */
  457.  if( (gsymt=hashtab[h].com_glob_symtab) == NULL) {
  458.     gsymt = install_global(h,type_COMMON_BLOCK,class_COMMON_BLOCK);
  459.     gsymt->info.comlist = NULL;
  460.  }
  461.  
  462.  
  463.  if( (symt = hashtab[h].com_loc_symtab) == NULL){
  464.     symt = install_local(h,type_COMMON_BLOCK,class_COMMON_BLOCK);
  465.     symt->info.toklist = NULL;
  466.  }
  467.  
  468.   /* Insert the new list onto linked list of token lists */
  469.  if(comlist != NULL) {
  470.     /* Will be NULL only for SAVE, in which case skip */
  471.      TH_ptr= make_TL_head(id);
  472.  
  473.       TH_ptr->tokenlist = comlist->next_token;
  474.      TH_ptr->next = symt->info.toklist;
  475.             symt->info.toklist = TH_ptr;
  476.  }
  477.  
  478.     symt->set_flag = TRUE;
  479.  symt->used_flag = TRUE;
  480. }/*def_com_block*/
  481.  
  482.  
  483. void
  484. def_com_variable(id)  /* Process items in common block list */
  485.  Token *id;
  486. {
  487.  int h=id->value.integer;
  488.  symtab *symt;
  489.  
  490.  if( (symt=hashtab[h].loc_symtab) == NULL) {
  491.     symt = install_local(h,type_UNDECL,class_VAR);
  492.  }
  493.  else {           /* Symbol has been seen before: check it */
  494.      if(symt->common_var) {
  495.   syntax_error(id->line_num,id->col_num,
  496.        "Variable cannot be in two different common blocks");
  497.      }
  498.      else if(symt->entry_point || symt->parameter ||
  499.       symt->argument || symt->external || symt->intrinsic) {
  500.   syntax_error(id->line_num,id->col_num,
  501.        "Item cannot be placed in common");
  502.      }
  503.  }
  504.     {  /* set flags for all equivalenced vars */
  505.       symtab *equiv=symt;
  506.       do{
  507.  equiv->common_var = TRUE; /* set the flag even if not legit */
  508.  equiv = equiv->equiv_link;
  509.       } while(equiv != symt);
  510.     }
  511.  
  512. }/*def_com_variable*/
  513.  
  514.  
  515.  /* This guy sets the flag in symbol table saying the id is the
  516.     current module.  It returns the hash code for later reference.
  517.   */
  518. int
  519. def_curr_module(id)
  520.  Token *id;
  521. {
  522.  int hashno = id->value.integer;
  523.  hashtab[hashno].loc_symtab->is_current_module = TRUE;
  524.  
  525.  return hashno;
  526. }/*def_curr_module*/
  527.  
  528.  
  529.  
  530.  
  531. void
  532. def_equiv_name(id)  /* Process equivalence list elements */
  533.  Token *id;
  534. {
  535.   ref_variable(id);  /* Put it in symtab */
  536.  /* No other action needed: processing of equiv pairs is
  537.     done by equivalence() */
  538. }/*def_equiv_name*/
  539.  
  540.  
  541.  
  542. void
  543. def_ext_name(id)  /* Process external lists */
  544.  Token *id;
  545. {
  546.  int h=id->value.integer;
  547.  symtab *symt;
  548.  
  549.  if( (symt = hashtab[h].loc_symtab) == NULL){
  550.     symt = install_local(h,type_UNDECL,class_SUBPROGRAM);
  551.     symt->info.toklist = NULL;
  552.         }
  553.  else {
  554.    /* Symbol seen before: check it & change class */
  555.  
  556.      if(storage_class_of(symt->type) == class_VAR) {
  557.        symt->info.toklist = NULL;
  558.      }
  559.      symt->type = type_byte(class_SUBPROGRAM,datatype_of(symt->type));
  560.  }
  561.  
  562.  if(symt->intrinsic){
  563.      syntax_error(id->line_num,id->col_num,
  564.   "Cannot declare same subprogram both intrinsic and external:");
  565.      msg_tail(symt->name);
  566.  }
  567.  else{
  568.      symt->external = TRUE;
  569.      if(!symt->argument){
  570.          TokenListHeader *TH_ptr;
  571.   symtab *gsymt;
  572.   if( (gsymt=hashtab[h].glob_symtab) == NULL) {
  573.          gsymt = install_global(h,type_UNDECL,class_SUBPROGRAM);
  574.          gsymt->info.arglist = NULL;
  575.   }
  576.   TH_ptr=make_TL_head(id);
  577.  
  578.   TH_ptr->external_decl = TRUE;
  579.   TH_ptr->next = symt->info.toklist;
  580.   symt->info.toklist = TH_ptr;
  581.       }
  582.    }
  583.       symt->declared_external = TRUE;
  584. }/*def_ext_name*/
  585.  
  586.  
  587.  
  588. void
  589. def_function(datatype,id,args)
  590.     /* Installs function or subroutine name */
  591.  int datatype;                     /* in global table */
  592.  Token *id,*args;
  593. {
  594.  int storage_class;
  595.  int h=id->value.integer;
  596.  symtab *symt,*gsymt;
  597.  TokenListHeader *TH_ptr;
  598.     storage_class = class_SUBPROGRAM;
  599.  
  600.  if((gsymt = (hashtab[h].glob_symtab)) == NULL) {
  601.    /* Symbol is new to global symtab: install it */
  602.    gsymt = install_global(h,datatype,storage_class);
  603.    gsymt->info.arglist = NULL;
  604.  }
  605.  else {
  606.    /* Symbol is already in global symtab. Put the
  607.       declared datatype into symbol table. */
  608.    gsymt->type = type_byte(storage_class,datatype);
  609.  }
  610.  
  611.     if((symt = (hashtab[id->value.integer].loc_symtab)) == NULL) {
  612.    /* Symbol is new to local symtab: install it.
  613.       Since this is the current routine, it has
  614.       storage class of a variable. */
  615.     symt = install_local(h,datatype,class_VAR);
  616.  }
  617.  if(! symt->entry_point) /* seen before but not as entry */
  618.     symt->info.toklist = NULL;
  619.  
  620.  
  621.   /* Insert the new list onto linked list of token lists */
  622.     TH_ptr=make_TL_head(id);
  623.  
  624.  TH_ptr->tokenlist = (args == NULL ? NULL: args->next_token);
  625.  TH_ptr->next = symt->info.toklist;
  626.  symt->info.toklist = TH_ptr;
  627.  
  628.  symt->entry_point = TRUE;
  629.  
  630.   /* library mode: set the flag so no complaint will
  631.      be issued if function never invoked.  Also, set
  632.      used_flag if this is a main program, for same reason. */
  633.  if(library_mode)
  634.   symt->library_module = TRUE;
  635.  if(datatype == type_PROGRAM)
  636.   symt->used_flag = TRUE;
  637. }/*def_function*/
  638.  
  639.  
  640.  
  641. void
  642. def_intrins_name(id)  /* Process intrinsic lists */
  643.  Token *id;
  644. {
  645.  int h=id->value.integer;
  646.  symtab *symt;
  647.  
  648.  if( (symt = hashtab[h].loc_symtab) == NULL){
  649.     symt = install_local(h,type_UNDECL,class_SUBPROGRAM);
  650.     symt->info.toklist = NULL;
  651.         }
  652.  else {
  653.    /* Symbol seen before: check it & change class */
  654.    if(storage_class_of(symt->type) == class_VAR) {
  655.      symt->info.toklist = NULL;
  656.    }
  657.  
  658.    symt->type = type_byte(class_SUBPROGRAM,datatype_of(symt->type));
  659.  }
  660.  
  661.   /* Place info about intrinsic datatype in local symtab.
  662.      If not found, it will be treated as external.
  663.    */
  664.  
  665.  if(symt->external){
  666.      syntax_error(id->line_num,id->col_num,
  667.         "Cannot declare same subprogram both intrinsic and external:");
  668.      msg_tail(symt->name);
  669.  }
  670.  else{
  671.    IntrinsInfo *defn;
  672.    if( (defn=find_intrinsic(symt->name)) == NULL ) {
  673.       warning(id->line_num,id->col_num,
  674.    "Unknown intrinsic function: ");
  675.       msg_tail(symt->name);
  676.       msg_tail("\nTreated as if user-defined");
  677.     /* Here treat as if EXTERNAL declaration */
  678.       def_ext_name(id);
  679.       return;
  680.     }
  681.     else {
  682.    /* Found in info table: set intrins flag and store
  683.       pointer to definition info. */
  684.       symt->intrinsic = TRUE;
  685.       symt->info.intrins_info = defn;
  686.     }
  687.  }
  688.  symt->declared_external = TRUE;
  689. }/*def_intrins_name*/
  690.  
  691. void
  692. def_parameter(id,val)  /* Process parameter_defn_item */
  693.  Token *id,*val;
  694. {
  695.  int h=id->value.integer;
  696.  symtab *symt;
  697.  
  698.  if( (symt=hashtab[h].loc_symtab) == NULL) {
  699.     symt = install_local(h,type_UNDECL,class_VAR);
  700.  }
  701.  
  702.  symt->set_flag = TRUE;
  703.  symt->parameter = TRUE;
  704.  if(incdepth > 0)
  705.    symt->defined_in_include = TRUE;
  706.  
  707.   /* Integer parameters: save value in symtab entry.  Other
  708.      types not saved.  Need these since used in array dims */
  709.  switch(get_type(symt)) {
  710.   case type_INTEGER:
  711.    symt->info.int_value = int_expr_value(val);
  712.    break;
  713.   default:
  714.    break;
  715.  }
  716. }/*def_parameter*/
  717.  
  718.  
  719.  
  720. void            /* Installs statement function name in local table */
  721. def_stmt_function(id, args)
  722.  Token *id, *args;
  723. {
  724.  int t,h=id->value.integer;
  725.  symtab *symt;
  726.     TokenListHeader *TH_ptr;
  727.  
  728.     if((symt = (hashtab[h].loc_symtab)) == NULL) {
  729.    /* Symbol is new to local symtab: install it. */
  730.  
  731.     symt = install_local(h,type_UNDECL,class_STMT_FUNCTION);
  732.     symt->info.toklist = NULL;
  733.  }
  734.  else {
  735.    if(storage_class_of(symt->type) == class_VAR) {
  736.      symt->info.toklist = NULL;
  737.    }
  738.  }
  739.  
  740.   /* Save dummy arg list in symbol table */
  741.      TH_ptr= make_TL_head(id);
  742.  
  743.  TH_ptr->tokenlist = (args == NULL ? NULL: args->next_token);
  744.  TH_ptr->next = symt->info.toklist;
  745.  symt->info.toklist = TH_ptr;
  746.  
  747.   /* Reverse the token list for sake of checking phase */
  748.  TH_ptr->tokenlist = reverse_tokenlist(TH_ptr->tokenlist);
  749.  
  750.  t=datatype_of(symt->type);
  751.   /* Symbol seen before: check it & change class */
  752.  
  753.   /* check, check, check ... */
  754.  if(storage_class_of(symt->type) == class_VAR)
  755.     symt->type = type_byte(class_STMT_FUNCTION,t);
  756.  
  757.  symt->external = TRUE;
  758. }/*def_stmt_function*/
  759.  
  760.  
  761.  
  762.  
  763. void
  764. do_ASSIGN(id)  /* Process ASSIGN statement */
  765.  Token *id;
  766. {
  767.  int h=id->value.integer;
  768.  symtab *symt;
  769.  
  770.  if( (symt=hashtab[h].loc_symtab) == NULL) {
  771.     symt = install_local(h,type_UNDECL,class_VAR);
  772.  }
  773.  else {
  774.     if(get_type(symt) != type_INTEGER) {
  775.        syntax_error(id->line_num,id->col_num,
  776.   "Variable must be an integer: ");
  777.        msg_tail(symt->name);
  778.     }
  779.  }
  780.     {  /* set flags for all equivalenced vars */
  781.       symtab *equiv=symt;
  782.       do{
  783.  equiv->set_flag = TRUE;
  784.  equiv = equiv->equiv_link;
  785.       } while(equiv != symt);
  786.     }
  787. }/*do_ASSIGN*/
  788.  
  789.  
  790.  
  791.  
  792. void
  793. do_assigned_GOTO(id)  /* Process assigned_goto */
  794.  Token *id;
  795. {
  796.  int h=id->value.integer;
  797.  symtab *symt;
  798.  
  799.  if( (symt=hashtab[h].loc_symtab) == NULL) {
  800.     symt = install_local(h,type_UNDECL,class_VAR);
  801.  }
  802.  else {
  803.     if(get_type(symt) != type_INTEGER) {
  804.        syntax_error(id->line_num,id->col_num,
  805.   "Variable must be an integer: ");
  806.        msg_tail(symt->name);
  807.     }
  808.  }
  809.     {  /* set flags for all equivalenced vars */
  810.       symtab *equiv=symt;
  811.       do{
  812.  if(! equiv->set_flag)
  813.     equiv->used_before_set = TRUE;
  814.  equiv->used_flag = TRUE;
  815.  equiv = equiv->equiv_link;
  816.       } while(equiv != symt);
  817.     }
  818.  
  819. }/*do_assigned_GOTO*/
  820.  
  821.  
  822.  
  823.  
  824.  
  825. void
  826. do_ENTRY(id,args,hashno) /* Processes ENTRY statement */
  827.  Token *id,*args;
  828.  int hashno;
  829. {
  830.  int datatype;
  831.  if(hashno == -1) { /* -1 signifies headerless program */
  832.      datatype = type_PROGRAM;
  833.  }
  834.  else {
  835.      datatype = datatype_of(hashtab[hashno].loc_symtab->type);
  836.  }
  837.  switch(datatype) {
  838.      case type_PROGRAM:
  839.      case type_BLOCK_DATA:
  840.      case type_COMMON_BLOCK:
  841.          syntax_error(id->line_num,NO_COL_NUM,
  842.    "You cannot have an entry statement here");
  843.   break;
  844.      case type_SUBROUTINE: /* Subroutine entry */
  845.   def_function(type_SUBROUTINE,id,args);
  846.   break;
  847.      default:  /* Function entry */
  848.   def_function(type_UNDECL,id,args);
  849.   break;
  850.  }
  851. }/*do_ENTRY*/
  852.  
  853.  
  854.  
  855.  
  856.  /* This routine checks whether a RETURN statement is valid at
  857.     the present location, and if it is, looks for possible
  858.     failure to assign return value of function.
  859.  */
  860. void
  861. do_RETURN(hashno,keyword)
  862.  int hashno; /* current module hash number */
  863.  Token *keyword; /* tok_RETURN, or tok_END if implied RETURN */
  864. {
  865.  int i,datatype;
  866.  if(hashno == -1) { /* -1 signifies headerless program */
  867.      datatype = type_PROGRAM;
  868.  }
  869.  else {
  870.      datatype = datatype_of(hashtab[hashno].loc_symtab->type);
  871.  }
  872.  switch(datatype) {
  873.      case type_PROGRAM:
  874.      case type_BLOCK_DATA:
  875.   if(keyword->class == tok_RETURN)
  876.       syntax_error(keyword->line_num,keyword->col_num,
  877.        "You cannot have a RETURN statement here!");
  878.   break;
  879.      case type_SUBROUTINE: /* Subroutine return: OK */
  880.   break;
  881.      default:  /* Function return: check whether entry
  882.        points have been assigned values. */
  883.   for(i=0; i<loc_symtab_top; i++) {
  884.       if(storage_class_of(loc_symtab[i].type) == class_VAR
  885.    && loc_symtab[i].entry_point
  886.    && ! loc_symtab[i].set_flag ) {
  887.        warning(keyword->line_num,keyword->col_num,
  888.      loc_symtab[i].name);
  889.        msg_tail("not set when RETURN encountered");
  890.       }
  891.   }
  892.   break;
  893.  }
  894.  
  895. }/*do_RETURN*/
  896.  
  897. void
  898. equivalence(id1,id2)
  899.      Token *id1, *id2;
  900. {
  901.  int h1=id1->value.integer, h2=id2->value.integer;
  902.  symtab *symt1,*symt2,*temp;
  903.  
  904.   /* install the variables in symtab if not seen before */
  905.  if( (symt1=hashtab[h1].loc_symtab) == NULL) {
  906.     symt1 = install_local(h1,type_UNDECL,class_VAR);
  907.  }
  908.  if( (symt2=hashtab[h2].loc_symtab) == NULL) {
  909.     symt2 = install_local(h2,type_UNDECL,class_VAR);
  910.  }
  911.    /* Check for legality.  Ought to do complementary
  912.       checks elsewhere.
  913.     */
  914.  if(symt1 == symt2
  915.     || symt1->parameter || symt2->parameter
  916.     || symt1->entry_point || symt2->entry_point
  917.     || symt1->argument || symt2->argument
  918.     || symt1->external || symt2->external) {
  919.  
  920.   syntax_error(id1->line_num,id1->col_num,
  921.         "illegal to equivalence these");
  922.  }
  923.   /* now swap equiv_links so their equiv lists are united */
  924.  else {
  925.      temp = symt1->equiv_link;
  926.      symt1->equiv_link = symt2->equiv_link;
  927.      symt2->equiv_link = temp;
  928.  }
  929.  
  930.   /* If either guy is in common, both are in common */
  931.  if(symt1->common_var || symt2->common_var) {
  932.      symtab *equiv=symt1;
  933.      do {
  934.   equiv->common_var = TRUE;
  935.   equiv = equiv->equiv_link;
  936.      } while(equiv != symt1);
  937.  }
  938. }
  939.  
  940. int
  941. get_type(symt) /* Returns data type of symbol, using implicit if necessary */
  942.  symtab *symt;
  943. {
  944.  int datatype = datatype_of(symt->type);
  945.  
  946.  if(datatype != type_UNDECL) /* Declared? */
  947.     return datatype;  /*   Yes: use it */
  948.  else if(storage_class_of(symt->type) == class_SUBPROGRAM
  949.       && !symt->invoked_as_func )
  950.     /* Function never invoked: assume subr */
  951.     return type_SUBROUTINE;
  952.  else   /* Otherwise use implicit type */
  953. #if ALLOW_UNDERSCORES
  954.     return (isupper((int)symt->name[0]))?
  955.       implicit_type[symt->name[0] - 'A']:
  956.         type_REAL; /* 1st char underscore => REAL */
  957. #else
  958.     return implicit_type[symt->name[0] - 'A'];
  959. #endif
  960. }/*get_type*/
  961.  
  962.  
  963.  /* hash_lookup finds identifier in hashtable and returns its
  964.     index.  If not found, a new hashtable entry is made for it,
  965.     and the identifier string s is copied to local stringspace.
  966.  */
  967. unsigned
  968. hash_lookup(s)
  969.  char *s;
  970. {
  971.         unsigned h;
  972.  unsigned long hnum;
  973.  
  974.  hnum = hash(s);
  975.  
  976.  while(h = hnum%HASHSZ, hashtab[h].name != NULL
  977.            && strcmp(hashtab[h].name,s) != 0) {
  978.      hnum = rehash(hnum); /* Resolve clashes */
  979.  }
  980.  
  981.  if(hashtab[h].name == NULL) {
  982.       hashtab[h].name = new_local_string(s);
  983.       hashtab[h].loc_symtab = NULL;
  984.       hashtab[h].glob_symtab = NULL;
  985.       hashtab[h].com_loc_symtab = NULL;
  986.       hashtab[h].com_glob_symtab = NULL;
  987.         }
  988.  return h;
  989. }/*hash_lookup*/
  990.  
  991. void
  992. init_globals()                 /* Clears the global symbol table */
  993. {
  994.  glob_str_bot = STRSPACESZ;
  995. }/*init_globals*/
  996.  
  997.  
  998.  
  999. void
  1000. init_symtab()                     /* Clears the local symbol table */
  1001. {
  1002.  int i,h;
  1003.  unsigned long hnum;
  1004.  
  1005.  loc_symtab_top = 0;
  1006.  loc_str_top = 0;
  1007.  token_space_top = 0;
  1008.  
  1009.         /* Clears the hash table */
  1010.  for(i=0;i<HASHSZ;i++) {
  1011.      hashtab[i].name = NULL;
  1012.      hashtab[i].loc_symtab = NULL;
  1013.      hashtab[i].com_loc_symtab = NULL;
  1014.      hashtab[i].glob_symtab = NULL;
  1015.      hashtab[i].com_glob_symtab = NULL;
  1016.  }
  1017.  
  1018.         /* Re-establishes global symbols */
  1019.  for(i=0;i<glob_symtab_top;i++) {
  1020.      hnum = hash(glob_symtab[i].name);
  1021.      while (h=hnum % HASHSZ, hashtab[h].name != NULL
  1022.         && strcmp(hashtab[h].name,glob_symtab[i].name) != 0 ) {
  1023.         hnum = rehash(hnum);
  1024.      }
  1025.      hashtab[h].name = glob_symtab[i].name;
  1026.      if(storage_class_of(glob_symtab[i].type) == class_COMMON_BLOCK)
  1027.   hashtab[h].com_glob_symtab = &(glob_symtab[i]);
  1028.      else
  1029.   hashtab[h].glob_symtab = &(glob_symtab[i]);
  1030.  
  1031.  }
  1032.  
  1033.         /* Restores implicit typing to default values */
  1034.  {
  1035.   int c;
  1036.   for( c=0; c<26; c++ )
  1037.           implicit_type[c] = type_REAL;
  1038.   for( c='I'-'A'; c <= 'N'-'A'; c++ )
  1039.       implicit_type[c] = type_INTEGER;
  1040.  }
  1041. }/*init_symtab*/
  1042.  
  1043.  
  1044.  
  1045. symtab*
  1046. install_global(h,datatype,storage_class) /* Install a global symbol */
  1047.  int h;   /* hash index */
  1048.  int datatype,storage_class;
  1049. {
  1050.  symtab *gsymt = &glob_symtab[glob_symtab_top];
  1051.  
  1052.  if(glob_symtab_top == GLOBSYMTABSZ) {
  1053.   fprintf(stderr,
  1054.    "\nOops! out of space in global symbol table.\n");
  1055.   exit(1);
  1056.  }
  1057.  else {
  1058.    /* Store symtab pointer in hash table */
  1059.      if(storage_class == class_COMMON_BLOCK)
  1060.   hashtab[h].com_glob_symtab = gsymt;
  1061.      else
  1062.   hashtab[h].glob_symtab = gsymt;
  1063.  
  1064.     /* Duplicate copy of string into global stringspace */
  1065.      gsymt->name = new_global_string(hashtab[h].name);
  1066.  
  1067.    /* Set symtab info fields */
  1068.      gsymt->type = type_byte(storage_class,datatype);
  1069.      if(storage_class == class_COMMON_BLOCK)
  1070.   gsymt->info.comlist = NULL;
  1071.      else
  1072.   gsymt->info.arglist = NULL;
  1073.      clear_symtab_flags(gsymt);
  1074.  
  1075.      ++glob_symtab_top;
  1076.  }
  1077.  return (gsymt);
  1078. }/*install_global*/
  1079.  
  1080.  
  1081. symtab*
  1082. install_local(h,datatype,storage_class) /* Install a local symbol */
  1083.  int h;   /* hash index */
  1084.  int datatype,storage_class;
  1085. {
  1086.  symtab *symt = &loc_symtab[loc_symtab_top];
  1087.  if(loc_symtab_top == LOCSYMTABSZ) {
  1088.   fprintf(stderr,
  1089.    "\nOops! out of space in local symbol table.\n");
  1090.   exit(1);
  1091.  }
  1092.  else {
  1093.      if(storage_class == class_COMMON_BLOCK)
  1094.   hashtab[h].com_loc_symtab = symt;
  1095.      else
  1096.   hashtab[h].loc_symtab = symt;
  1097.      symt->name = hashtab[h].name;
  1098.      symt->info.array_dim = 0;
  1099.  
  1100.         /* Set symtab info fields */
  1101.      symt->type = type_byte(storage_class,datatype);
  1102.      symt->equiv_link = symt; /* equivalenced only to self */
  1103.      clear_symtab_flags(symt);
  1104.      ++loc_symtab_top;
  1105.  }
  1106.  return symt;
  1107. }/*install_local*/
  1108.  
  1109.  
  1110.   /* Get value specified by an integer-expression token.
  1111.      This will be either an identifier, which should be a
  1112.      parameter whose value is in the symbol table, or else
  1113.      an expression token as propagated by exprtype.c
  1114.      routines, with value stored in the token.
  1115.   */
  1116. int
  1117. int_expr_value(t)
  1118.  Token *t;
  1119. {
  1120.     if(! is_true(CONST_EXPR,t->subclass) ) {
  1121.  syntax_error(t->line_num,t->col_num,"constant expression required");
  1122.  return 0;
  1123.     }
  1124.     else {
  1125.  if( is_true(ID_EXPR,t->subclass) ) {
  1126.   /* Identifier: better be a parameter */
  1127.      int h=t->value.integer;
  1128.      symtab *symt = hashtab[h].loc_symtab;
  1129.      if(symt == NULL || !(symt->parameter) ) {
  1130.   syntax_error(t->line_num,t->col_num,
  1131.    "constant expression required");
  1132.   return 0;
  1133.      }
  1134.      else {
  1135.   return symt->info.int_value;
  1136.      }
  1137.  }
  1138.   /* Otherwise, it is a const or expr, use token.value.integer */
  1139.  else {
  1140.      return t->value.integer;
  1141.  }
  1142.     }
  1143. }/*int_expr_value*/
  1144.  
  1145.  
  1146.  /* Following routine converts a list of tokens into a list of type-
  1147.     flag pairs. */
  1148.  
  1149. PRIVATE ArgListHeader *
  1150. make_arg_array(t)
  1151.  Token *t;  /* List of tokens */
  1152. {
  1153.  int i;
  1154.  unsigned count;
  1155.  Token *s;
  1156.  ArgListElement *arglist;
  1157.  ArgListHeader *alhead;
  1158.  
  1159.  count = arg_count(t);
  1160.  if(((alhead=(ArgListHeader *) calloc(1, sizeof(ArgListHeader)))
  1161.       == (ArgListHeader *) NULL) ||
  1162.    (count != 0 &&
  1163.           ((arglist=(ArgListElement *) calloc(count,sizeof(ArgListElement)))
  1164.      == (ArgListElement *) NULL))){
  1165.   fprintf(stderr, "Out of space for argument list");
  1166.   exit(1);
  1167.  }
  1168.  s = t;            /* List of tokens is in reverse order. */
  1169.  for(i=count-1; i>=0; i--){  /* Here we fill array in original order. */
  1170.  
  1171.      arglist[i].type = s->class; /* use evaluated type, not symt */
  1172.  
  1173.    /* Keep track of array and external declarations */
  1174.      if( is_true(ID_EXPR,s->subclass) ){
  1175.   int h = s->value.integer;
  1176.   symtab *symt = hashtab[h].loc_symtab;
  1177.   if( (arglist[i].info.array_dim = symt->info.array_dim) == 0)
  1178.     /* change scalars to 0 dims, size 1 */
  1179.     arglist[i].info.array_dim = array_dim_info(0,1);
  1180.   arglist[i].array_var = symt->array_var;
  1181.   arglist[i].declared_external = symt->declared_external;
  1182.      }
  1183.      else {
  1184.   arglist[i].info.array_dim = 0;
  1185.   arglist[i].array_var = FALSE;
  1186.   arglist[i].declared_external = FALSE;
  1187.      }
  1188.  
  1189.      arglist[i].array_element =
  1190.   arglist[i].array_var && !is_true(ARRAY_ID_EXPR,s->subclass);
  1191.  
  1192.      if( is_true(LVALUE_EXPR,s->subclass) ){
  1193.   arglist[i].is_lvalue = TRUE;
  1194.    /* is_true(f,x) yields 0 or non-0: convert to 0 or 1 */
  1195.   arglist[i].set_flag =
  1196.    is_true(SET_FLAG,s->subclass)? TRUE: FALSE;
  1197.   arglist[i].assigned_flag =
  1198.    is_true(ASSIGNED_FLAG,s->subclass)? TRUE: FALSE;
  1199.   arglist[i].used_before_set =
  1200.    is_true(USED_BEFORE_SET,s->subclass)? TRUE: FALSE;
  1201.      }
  1202.      else { /* it is an expression or constant, not an lvalue */
  1203.   arglist[i].is_lvalue = FALSE;
  1204.   arglist[i].set_flag = TRUE;
  1205.   arglist[i].assigned_flag = FALSE;
  1206.   arglist[i].used_before_set = FALSE;
  1207.      }
  1208.      s = s->next_token;
  1209.  }
  1210.  alhead->numargs = count;
  1211.  alhead->is_defn = FALSE;
  1212.  alhead->is_call = TRUE;
  1213.  alhead->external_decl = FALSE;
  1214.  alhead->actual_arg = FALSE;
  1215.  
  1216.         if (count == 0)
  1217.   alhead->arg_array = NULL;
  1218.  else
  1219.   alhead->arg_array = arglist;
  1220.  return(alhead);
  1221. }/* make_arg_array */
  1222.  
  1223.  
  1224.  /* Following routine converts a list of common block tokens
  1225.      into a list of dimen_info-type pairs. */
  1226.  
  1227. PRIVATE ComListHeader *
  1228. make_com_array(t)
  1229.  Token *t;  /* List of tokens */
  1230. {
  1231.  Token *s;
  1232.  symtab *symt;
  1233.  int h, i;
  1234.  unsigned count;
  1235.  ComListHeader *clhead;
  1236.  ComListElement *comlist;
  1237.  
  1238.  count = arg_count(t);
  1239.  if(((clhead=(ComListHeader *) calloc(1,sizeof(ComListHeader)))
  1240.    == (ComListHeader *) NULL) ||
  1241.    (count != 0 &&
  1242.     ((comlist=(ComListElement *) calloc(count,sizeof(ComListElement)))
  1243.    == (ComListElement *) NULL))){
  1244.   fprintf(stderr, "Out of space for common list");
  1245.   exit(1);
  1246.  }
  1247.  s = t;
  1248.  for(i=count-1; i>=0; i--){
  1249.     h = s->value.integer;
  1250.     symt = hashtab[h].loc_symtab;
  1251.     if( (comlist[i].dimen_info = symt->info.array_dim) == 0)
  1252.     /* change scalars to 0 dims, size 1 */
  1253.       comlist[i].dimen_info = array_dim_info(0,1);
  1254.     comlist[i].type = get_type(symt);
  1255.     s = s->next_token;
  1256.  }
  1257.  clhead->numargs = count;
  1258.  if (count == 0)
  1259.   clhead->com_list_array = NULL;
  1260.  else
  1261.   clhead->com_list_array = comlist;
  1262.  return(clhead);
  1263. } /* make_com_array */
  1264.  
  1265.  
  1266. PRIVATE ArgListHeader *
  1267. make_dummy_arg_array (t)
  1268.  Token *t;  /* List of tokens */
  1269. {
  1270.  int i;
  1271.  unsigned count;
  1272.  Token *s;
  1273.  ArgListElement *arglist;
  1274.  ArgListHeader *alhead;
  1275.  
  1276.  count = arg_count(t);
  1277.  if(((alhead=(ArgListHeader *) calloc(1, sizeof(ArgListHeader)))
  1278.     == (ArgListHeader *) NULL) ||
  1279.    (count != 0 &&
  1280.           ((arglist=(ArgListElement *) calloc(count,sizeof(ArgListElement)))
  1281.    == (ArgListElement *) NULL))){
  1282.   fprintf(stderr, "Out of space for argument list");
  1283.   exit(1);
  1284.  }
  1285.  s = t;            /* List of tokens is in reverse order. */
  1286.  for(i=count-1; i>=0; i--){  /* Here we fill array in original order. */
  1287.      if( is_true(ID_EXPR,s->subclass) ){
  1288.   int h = s->value.integer;
  1289.   symtab *symt = hashtab[h].loc_symtab;
  1290.   if( (arglist[i].info.array_dim = symt->info.array_dim) == 0)
  1291.     /* change scalars to 0 dims, size 1 */
  1292.     arglist[i].info.array_dim = array_dim_info(0,1);
  1293.   arglist[i].type = type_byte(storage_class_of(symt->type),
  1294.       get_type(symt));
  1295.   arglist[i].is_lvalue = TRUE;
  1296.   arglist[i].set_flag = symt->set_flag;
  1297.   arglist[i].assigned_flag = symt->assigned_flag;
  1298.   arglist[i].used_before_set = symt->used_before_set;
  1299.   arglist[i].array_var = symt->array_var;
  1300.   arglist[i].array_element = FALSE;
  1301.   arglist[i].declared_external = symt->declared_external;
  1302.      }
  1303.      else { /* It is a label */
  1304.   arglist[i].info.array_dim = 0;
  1305.   arglist[i].type = s->class;
  1306.   arglist[i].is_lvalue = FALSE;
  1307.   arglist[i].set_flag = FALSE; /* Don't currently do labels */
  1308.   arglist[i].assigned_flag = FALSE;
  1309.   arglist[i].used_before_set = FALSE;
  1310.   arglist[i].array_var = FALSE;
  1311.   arglist[i].array_element = FALSE;
  1312.   arglist[i].declared_external = FALSE;
  1313.      }
  1314.      s = s->next_token;
  1315.  }
  1316.  alhead->numargs = count;
  1317.  alhead->is_defn = TRUE;
  1318.  alhead->is_call = FALSE;
  1319.  alhead->external_decl = FALSE;
  1320.  alhead->actual_arg = FALSE;
  1321.  
  1322.         if (count == 0)
  1323.   alhead->arg_array = NULL;
  1324.  else
  1325.   alhead->arg_array = arglist;
  1326.  return(alhead);
  1327. }/* make_dummy_arg_array */
  1328.  
  1329.  
  1330.  /* This routine makes an empty argument list: used for
  1331.     EXTERNAL declarations of subprograms. */
  1332. PRIVATE ArgListHeader *
  1333. make_arrayless_alist()
  1334. {
  1335.  ArgListHeader *alhead;
  1336.  
  1337.  if(((alhead=(ArgListHeader *) calloc(1, sizeof(ArgListHeader)))
  1338.       == (ArgListHeader *) NULL) ) {
  1339.   fprintf(stderr, "Out of space for external decl\n");
  1340.   exit(1);
  1341.  }
  1342.  
  1343.  alhead->numargs = 0;
  1344.  alhead->is_defn = FALSE;
  1345.  alhead->is_call = FALSE;
  1346.  alhead->arg_array = NULL;
  1347.  
  1348.  return(alhead);
  1349. }/* make_arrayless_arglist */
  1350.  
  1351. PRIVATE TokenListHeader * /* Initializes a tokenlist header */
  1352. make_TL_head(t)
  1353.      Token *t;
  1354. {
  1355.   TokenListHeader *TH_ptr;
  1356.  
  1357.        if((TH_ptr=(TokenListHeader *) calloc(1,sizeof(TokenListHeader)))
  1358.      == (TokenListHeader *) NULL){
  1359.     fprintf(stderr,"Out of space for token list");
  1360.     exit(1);
  1361.  }
  1362.  
  1363.  TH_ptr->line_num = t->line_num;
  1364.    TH_ptr->filename = current_filename;
  1365.     /* Clear all the flags */
  1366.  TH_ptr->external_decl = FALSE;
  1367.  TH_ptr->actual_arg = FALSE;
  1368.  TH_ptr->tokenlist = NULL;
  1369.  TH_ptr->next = NULL;
  1370.  
  1371.   return TH_ptr;
  1372. }
  1373.  
  1374.   /* this routine allocates room in global part (top down)
  1375.      of stringspace for string s, and copies it there */
  1376. char *
  1377. new_global_string(s)
  1378.  char *s;
  1379. {
  1380.  glob_str_bot -= strlen(s) + 1;    /*pre-decrement*/
  1381.  if( glob_str_bot < loc_str_top ) {
  1382.      fprintf(stderr,"\noops: out of global stringspace.\n");
  1383.      exit(1);
  1384.  }
  1385.  return strcpy(strspace+glob_str_bot,s);
  1386. }/*new_global_string*/
  1387.  
  1388.   /* Allocate space for string s in local (bottom up)
  1389.      string space, and copy it there */
  1390. char *
  1391. new_local_string(s)
  1392.  char *s;
  1393. {
  1394.  char *start = strspace + loc_str_top;
  1395.  loc_str_top += strlen(s) + 1; /* post-increment */
  1396.  if(loc_str_top > glob_str_bot) {
  1397.      fprintf(stderr,"\noops: out of stringspace\n");
  1398.       exit(1);
  1399.  }
  1400.  
  1401.  return strcpy(start,s);
  1402. }/* new_local_string */
  1403.  
  1404. Token *
  1405. new_token()   /* Returns pointer to space for a token */
  1406. {
  1407.   if(token_space_top == TOKENSPACESZ)
  1408.     return (Token *)NULL;
  1409.   else
  1410.     return tokenspace + token_space_top++;
  1411. }
  1412.  
  1413.  /* note_filename():  This routine is called by main prog to give
  1414.     symbol table routines access to current input file name, to be
  1415.     stored in function arg list headers and common list headers, for
  1416.     the use in diagnostic messages. Since filenames are from argv,
  1417.     they are permanent, so pointer is copied, not the string.
  1418.  */
  1419. void
  1420. note_filename(s)
  1421.  char *s;
  1422. {
  1423.  current_filename = s;
  1424.  top_filename = s;
  1425. }/* note_filename */
  1426.  
  1427.  
  1428.  
  1429. void
  1430. process_lists(curmodhash)  /* Places pointer to linked list of arrays in
  1431.          global symbol table */
  1432.  int curmodhash;    /* current_module_hash from fortran.y */
  1433. {
  1434.  int i, h;
  1435.  unsigned long hnum;
  1436.  symtab *gsymt;
  1437.  TokenListHeader *head_ptr;
  1438.  
  1439.  for (i=0; i<loc_symtab_top; i++){
  1440.     /* Skip things which are not true externals */
  1441.      if(loc_symtab[i].argument || loc_symtab[i].intrinsic ||
  1442.      loc_symtab[i].array_var)
  1443.         continue;
  1444.  
  1445.      head_ptr = loc_symtab[i].info.toklist;
  1446.  
  1447.      hnum=hash(loc_symtab[i].name);
  1448.      while(h=hnum%HASHSZ,hashtab[h].name != NULL
  1449.    && strcmp(hashtab[h].name,loc_symtab[i].name)!=0){
  1450.         hnum = rehash(hnum);      /* Resolve clashes */
  1451.      }
  1452.  
  1453.      switch (storage_class_of(loc_symtab[i].type)){
  1454.       case class_COMMON_BLOCK:
  1455.    if(head_ptr != NULL) {
  1456. if((gsymt=hashtab[h].com_glob_symtab) == NULL)
  1457.     fprintf(stderr,"\nOops! common block %s not in global symtab",
  1458.  loc_symtab[i].name);
  1459. else {
  1460.    Token *tok_ptr;
  1461.                         ComListHeader *c;
  1462.  
  1463.     /* First we link up possibly multiple
  1464.        declarations of the same common block
  1465.        in this module into one big list */
  1466.        while (tok_ptr = head_ptr->tokenlist,
  1467.           (head_ptr = head_ptr->next) != NULL){
  1468.        while(tok_ptr->next_token != NULL){
  1469.            tok_ptr = tok_ptr->next_token;
  1470.        }
  1471.        tok_ptr->next_token = head_ptr->tokenlist;
  1472.    }
  1473.  
  1474.     /* Now make it into array for global table */
  1475.           c=make_com_array(loc_symtab[i].info.toklist->tokenlist);
  1476.    c->module = (curmodhash == -1) ? NULL:
  1477.         hashtab[curmodhash].glob_symtab;
  1478.    c->line_num = loc_symtab[i].info.toklist->line_num;
  1479.    c->filename = loc_symtab[i].info.toklist->filename;
  1480.    c->topfile = top_filename;
  1481.  
  1482.                         c->next = gsymt->info.comlist;
  1483.    gsymt->info.comlist = c;
  1484.   /* Replace token list by comlist for project file use */
  1485.    loc_symtab[i].info.comlist = c;
  1486. }
  1487.    }/* end if(head_ptr != NULL) */
  1488.  
  1489.           break; /* end case class_COMMON_BLOCK */
  1490.  
  1491.  
  1492.    /* Are we inside a function or subroutine? */
  1493.       case class_VAR:
  1494.          if(loc_symtab[i].entry_point) {
  1495. if((gsymt=hashtab[h].glob_symtab) == NULL)
  1496.     fprintf(stderr,"\nOops! subprog %s not in global symtab",
  1497.  loc_symtab[i].name);
  1498. else {
  1499.                           ArgListHeader *a;
  1500.  
  1501.     /* Make each token list into an array of
  1502.        args for global table */
  1503.      while (head_ptr != NULL){
  1504.         a=make_dummy_arg_array(head_ptr->tokenlist);
  1505.  
  1506.         a->type = type_byte(
  1507.             class_SUBPROGRAM,
  1508.      get_type(&(loc_symtab[i])));
  1509.         a->module = (curmodhash == -1) ? NULL:
  1510.         hashtab[curmodhash].glob_symtab;
  1511.         a->filename = head_ptr->filename;
  1512.         a->topfile = top_filename;
  1513.         a->line_num = head_ptr->line_num;
  1514.  
  1515.         a->next = gsymt->info.arglist;
  1516.         gsymt->info.arglist = a;
  1517.    /* store arglist in local symtab for project file */
  1518.         loc_symtab[i].info.arglist = a;
  1519.         head_ptr = head_ptr->next;
  1520.             }/* end while (head_ptr != NULL) */
  1521.  
  1522.      if(loc_symtab[i].set_flag)
  1523.             gsymt->set_flag = TRUE;
  1524.      if(loc_symtab[i].used_flag)
  1525.             gsymt->used_flag = TRUE;
  1526.      if(loc_symtab[i].declared_external)
  1527.      gsymt->declared_external = TRUE;
  1528.      if(loc_symtab[i].library_module)
  1529.      gsymt->library_module = TRUE;
  1530. }
  1531.    }/* end if(loc_symtab[i].entry_point) */
  1532.  
  1533.        break; /* end case class_VAR */
  1534.  
  1535.                     case class_SUBPROGRAM:
  1536. if((gsymt=hashtab[h].glob_symtab) == NULL)
  1537.     fprintf(stderr,"\nOops! subprog %s not in global symtab",
  1538.  loc_symtab[i].name);
  1539. else {
  1540.                         ArgListHeader *a;
  1541.    while (head_ptr != NULL){
  1542.      if(head_ptr->external_decl || head_ptr->actual_arg)
  1543.        a=make_arrayless_alist();
  1544.      else
  1545.        a=make_arg_array(head_ptr->tokenlist);
  1546.  
  1547.      a->type = type_byte(
  1548.             class_SUBPROGRAM,
  1549.      get_type(&(loc_symtab[i])));
  1550.      a->module = (curmodhash == -1) ? NULL:
  1551.         hashtab[curmodhash].glob_symtab;
  1552.      a->filename = head_ptr->filename;
  1553.      a->topfile = top_filename;
  1554.      a->line_num = head_ptr->line_num;
  1555.      a->external_decl = head_ptr->external_decl;
  1556.      a->actual_arg = head_ptr->actual_arg;
  1557.  
  1558.      a->next = gsymt->info.arglist;
  1559.      gsymt->info.arglist = a;
  1560.   /* put arglist into local symtab for project file use */
  1561.      loc_symtab[i].info.arglist = a;
  1562.      head_ptr = head_ptr->next;
  1563.           }
  1564.    if(loc_symtab[i].used_flag)
  1565.            gsymt->used_flag = TRUE;
  1566. if(debug_glob_symtab)
  1567. fprintf(list_fd,"\nmodule %s local used=%d global used=%d",
  1568. gsymt->name,loc_symtab[i].used_flag,gsymt->used_flag);
  1569. }
  1570.    break;/* end case class_SUBPROGRAM*/
  1571.  
  1572.      }/* end switch */
  1573.  
  1574.         }/* end for (i=0; i<loc_symtab_top; i++) */
  1575.  
  1576. }/* process_lists */
  1577.  
  1578.  
  1579. void
  1580. ref_array(id,subscrs)   /* Array reference: install in symtab */
  1581.  Token *id, *subscrs;
  1582. {
  1583.  int h=id->value.integer;
  1584.  symtab *symt=hashtab[h].loc_symtab;
  1585.  
  1586.  if(symt == NULL){
  1587.     fprintf(stderr, "\nOops -- undeclared variable %s has dim info",
  1588.        hashtab[h].name);
  1589.     symt = install_local(h,type_UNDECL,class_VAR);
  1590.  }
  1591.  else{    /* check that subscrs match dimension info */
  1592.  
  1593.  
  1594.    if(arg_count(subscrs->next_token)!=array_dims(symt->info.array_dim)){
  1595.        syntax_error(subscrs->line_num,subscrs->col_num,
  1596.    "array");
  1597.        msg_tail(symt->name);
  1598.        msg_tail("referenced with wrong no. of subscripts");
  1599.    }
  1600.  }
  1601. }/* ref_array */
  1602.  
  1603. void
  1604. ref_variable(id) /* Variable reference: install in symtab */
  1605.  Token *id;
  1606. {
  1607.  int h=id->value.integer;
  1608.  
  1609.  if( hashtab[h].loc_symtab == NULL) {
  1610.     (void) install_local(h,type_UNDECL,class_VAR);
  1611.  }
  1612.  
  1613. }/*ref_variable*/
  1614.  
  1615.   /* this guy reverses a tokenlist and returns a pointer
  1616.      to the new head. */
  1617. PRIVATE Token *
  1618. reverse_tokenlist(t)
  1619.  Token *t;
  1620. {
  1621.  Token *curr,*next,*temp;
  1622.  
  1623.  if(t == NULL)
  1624.      return t;
  1625.  
  1626.  curr = t;
  1627.  next = curr->next_token;
  1628.  while(next != NULL) {
  1629.   temp = next->next_token;
  1630.   next->next_token = curr;
  1631.   curr = next;
  1632.   next = temp;
  1633.  }
  1634.  t->next_token = NULL;  /* former head is now tail */
  1635.  return curr;   /* curr now points to new head */
  1636. }
  1637.  
  1638.  /* Following routine sets the implicit typing of characters in
  1639.     range c1 to c2 to the given type. */
  1640. void
  1641. set_implicit_type(type,c1,c2)
  1642.  int type,  /* Data type of IMPLICIT declaration */
  1643.      c1,   /* First character of range */
  1644.      c2;   /* Last character of range */
  1645. {
  1646.  int c;
  1647.  
  1648.  if(c2 < c1) {
  1649.   yyerror("IMPLICIT range must be in alphabetical order");
  1650.  }
  1651.  
  1652.   /* Fill in the lookup table for the given range of chars */
  1653.  for(c=c1; c<=c2; c++)
  1654.   implicit_type[c-'A'] = type;
  1655. }/*set_implicit_type*/
  1656.  
  1657.   /* Finish processing statement function.
  1658.      Clears all used-before-set flags of ordinary
  1659.      variables. Reason: statement functions are processed
  1660.      like assignment to an array element, setting ubs flags.
  1661.      At this point, no valid setting of ubs flags should
  1662.      be possible, so clearing them will elim false messages.*/
  1663. void
  1664. stmt_function_stmt(id)
  1665.      Token *id;
  1666. {
  1667.     int i;
  1668.     for(i=0; i<loc_symtab_top; i++) {
  1669.  if(storage_class_of(loc_symtab[i].type) == class_VAR &&
  1670.     ! loc_symtab[i].parameter )
  1671.    loc_symtab[i].used_before_set = FALSE;
  1672.     }
  1673. }/*stmt_function_stmt(id)*/
  1674.  
  1675. char *
  1676. token_name(t)
  1677.  Token t;
  1678. {
  1679.  return hashtab[t.value.integer].name;
  1680. }/*token_name*/
  1681.  
  1682.  
  1683.  
  1684.  
  1685. void
  1686. use_actual_arg(id) /* like use_lvalue except does not set assigned_flag */
  1687.  Token *id;
  1688. {
  1689.  int h=id->value.integer;
  1690.  symtab *symt;
  1691.  
  1692.  if((symt=hashtab[h].loc_symtab) == NULL) {
  1693.      symt = install_local(h,type_UNDECL,class_VAR);
  1694.  }
  1695.  else {
  1696.    /* if an external, set up tokenlist for "call"  */
  1697.    if(storage_class_of(symt->type) == class_SUBPROGRAM) {
  1698.        TokenListHeader *TH_ptr;
  1699.        TH_ptr= make_TL_head(id);
  1700.  
  1701.        TH_ptr->actual_arg = TRUE;
  1702.        TH_ptr->next = symt->info.toklist;
  1703.        symt->info.toklist = TH_ptr;
  1704.    }
  1705.  }
  1706.  
  1707.     {  /* set flags for all equivalenced vars */
  1708.       symtab *equiv=symt;
  1709.       do{
  1710.  equiv->set_flag = TRUE;
  1711.  equiv = equiv->equiv_link;
  1712.       } while(equiv != symt);
  1713.     }
  1714.  
  1715. }/*use_actual_arg*/
  1716.  
  1717. void
  1718. use_implied_do_index(id)
  1719.  Token *id;
  1720. {
  1721.   /* Like use_lvalue and use_variable but clears ubs flag.
  1722.             This is because we cannot handle used-before-set
  1723.      properly in this case, and the odds are that ubs
  1724.      was set in the preceding I/O list. */
  1725.  int h=id->value.integer;
  1726.  symtab *symt;
  1727.  
  1728.  use_lvalue(id);
  1729.  use_variable(id);
  1730.  symt=hashtab[h].loc_symtab;
  1731.  
  1732.  symt->used_before_set = FALSE;
  1733. }/*use_implied_do_index*/
  1734.  
  1735.  
  1736.  /* use_io_keyword handles keyword=value fields in i/o control lists */
  1737.  
  1738. #include "iokeywds.h"
  1739.  
  1740. void
  1741. use_io_keyword(keyword,value,stmt_class)
  1742.      Token *keyword,*value;
  1743.      int stmt_class;
  1744. {
  1745.     int i, k, stmt_flag=0, type_flag, setit,useit;
  1746.     int hkey=keyword->value.integer;
  1747.  
  1748.   /* Convert statement_class (a token class) into
  1749.      a bit flag compatible with io_keywords table. */
  1750.     for(i=0; i<NUM_IO_STMTS; i++) {
  1751.  if(local_class[i].stmt_class == stmt_class) {
  1752.      stmt_flag = local_class[i].stmt_flag;
  1753.      break;
  1754.  }
  1755.     }
  1756.     if(stmt_flag == 0) {
  1757.  fprintf(list_fd,"\nOops -- %d is not an i/o statement class",
  1758.   stmt_class);
  1759.  return;
  1760.     }
  1761.   /* Convert value datatype into
  1762.      a bit flag compatible with io_keywords table.
  1763.      Note that '*' is handled by using type_UNDECL */
  1764.     if(value->class == '*')
  1765.  type_flag = STAR;
  1766.     else
  1767.  type_flag = (1<<value->class);
  1768.  
  1769.     /* Look up keyword in table*/
  1770.     k = find_io_keyword(hashtab[hkey].name);
  1771.  
  1772.   /* Not found or nonstandard: issue warning.  Note
  1773.      that not-found is also nonstandard. */
  1774.     if(io_keywords[k].nonstandard
  1775. #ifdef VMS_IO /* special VMS case: OPEN(...,NAME=str,...) */
  1776.        || (io_keywords[k].special && stmt_flag==OP)
  1777. #endif /*VMS_IO*/
  1778.     ) {
  1779.   /* If nonstandard and -f77 flag given, issue warning */
  1780.  if(f77_standard) {
  1781.      nonstandard(keyword->line_num,keyword->col_num);
  1782.  }
  1783.  if(io_keywords[k].name == NULL) {
  1784.      if(f77_standard) { /* abbrev warning if nonstd message given */
  1785.   msg_tail(": unrecognized keyword");
  1786.      }
  1787.      else {
  1788.   warning(keyword->line_num,keyword->col_num,
  1789.   "Unrecognized keyword");
  1790.      }
  1791.      msg_tail(hashtab[hkey].name);
  1792.      msg_tail("--\n  Forchek may process incorrectly");
  1793.  }
  1794.     }
  1795.  
  1796.  /* If label expected, switch integer const to label */
  1797.     if( (LAB & io_keywords[k].allowed_types)
  1798.        &&  (type_flag == INT && is_true(NUM_CONST,value->subclass))) {
  1799.  type_flag = LAB;
  1800.     }
  1801.  
  1802.  /*  Now check it out */
  1803.  
  1804.  
  1805.   /* Check if keyword is allowed with statement */
  1806.  
  1807.     if(!(stmt_flag & io_keywords[k].allowed_stmts)) {
  1808.  syntax_error(keyword->line_num,keyword->col_num,
  1809.        "keyword illegal in this context");
  1810.     }
  1811.  
  1812.   /* Check if the type is OK */
  1813.  
  1814.     if( !(type_flag & io_keywords[k].allowed_types) ) {
  1815.  syntax_error(value->line_num,value->col_num,
  1816.        "control specifier is incorrect type");
  1817.     }
  1818.  
  1819.  
  1820.  /* Now handle usage */
  1821.  
  1822.     /* internal file?: WRITE(UNIT=str,...) */
  1823.     if(stmt_flag == WR && type_flag == CHR
  1824.      && io_keywords[k].allowed_types == UID) {
  1825.  setit = TRUE;
  1826.  useit = FALSE;
  1827.     }
  1828.     /* INQUIRE: set it if inquire_set flag true */
  1829.     else if(stmt_flag == INQ && io_keywords[k].inquire_set) {
  1830.  setit = TRUE;
  1831.  useit = FALSE;
  1832.     }
  1833.     /* otherwise use use/set flags in table */
  1834.     else {
  1835.  useit = io_keywords[k].implies_use;
  1836.  setit = io_keywords[k].implies_set;
  1837.     }
  1838.    /* Update usage status if a variable. */
  1839.     if(useit) {
  1840.  if( is_true(ID_EXPR,value->subclass)) {
  1841.      use_variable(value);
  1842.  }
  1843.     }
  1844.     if(setit) {   /* if value is set, must be an lvalue */
  1845.  if( is_true(ID_EXPR,value->subclass)) {
  1846.      use_lvalue(value);
  1847.  }
  1848.  else {
  1849.      syntax_error(value->line_num,value->col_num,
  1850.     "variable required");
  1851.  }
  1852.     }
  1853. }
  1854.  
  1855. void
  1856. use_lvalue(id) /* handles scalar lvalue */
  1857.  Token *id;
  1858. {
  1859.  int h=id->value.integer;
  1860.  symtab *symt;
  1861.  if((symt=hashtab[h].loc_symtab) == NULL) {
  1862.      symt = install_local(h,type_UNDECL,class_VAR);
  1863.  }
  1864.  else {
  1865.    /*   check match to previous invocations and update  */
  1866.  }
  1867.     {  /* set flags for all equivalenced vars */
  1868.       symtab *equiv=symt;
  1869.       do{
  1870.  equiv->set_flag = TRUE;
  1871.  equiv->assigned_flag = TRUE;
  1872.  equiv = equiv->equiv_link;
  1873.       } while(equiv != symt);
  1874.     }
  1875.  
  1876. }/*use_lvalue*/
  1877.  
  1878.  
  1879.  
  1880. void                    /* Process data_constant_value & data_repeat_factor */
  1881. use_parameter(id)
  1882.  Token *id;
  1883. {
  1884.  int h=id->value.integer;
  1885.  symtab *symt;
  1886.  
  1887.  if( (symt=hashtab[h].loc_symtab) == NULL) {
  1888.     symt = install_local(h,type_UNDECL,class_VAR);
  1889.  }
  1890.  if(! symt->parameter) {
  1891.   syntax_error(id->line_num,id->col_num,
  1892.    "must be a parameter");
  1893.   symt->parameter = TRUE;
  1894.  }
  1895.  
  1896.  if(! symt->set_flag) {
  1897.     symt->used_before_set = TRUE;
  1898.  }
  1899.  symt->used_flag = TRUE;
  1900.  
  1901. }/*use_parameter*/
  1902.  
  1903.  
  1904. void
  1905. use_var_as_subscr(id) /* Like use_variable but invokes use_actual_arg
  1906.       if id is an external.  This occurs when a
  1907.       subprogram is passed as arg of a function. */
  1908.  Token *id;
  1909. {
  1910.  int h=id->value.integer;
  1911.  symtab *symt;
  1912.  
  1913.  if( (symt=hashtab[h].loc_symtab) == NULL) {
  1914.     symt = install_local(h,type_UNDECL,class_VAR);
  1915.  }
  1916.  
  1917.  if(storage_class_of(symt->type) == class_SUBPROGRAM)
  1918.    use_actual_arg(id);
  1919.  else
  1920.    use_variable(id);
  1921.  
  1922. }/*use_var_as_subscr*/
  1923.  
  1924. void
  1925. use_variable(id)  /* Set the use-flag of variable. */
  1926.  Token *id;
  1927. {
  1928.  int h=id->value.integer;
  1929.  symtab *symt;
  1930.  
  1931.  if( (symt=hashtab[h].loc_symtab) == NULL) {
  1932.     symt = install_local(h,type_UNDECL,class_VAR);
  1933.  }
  1934.  
  1935.     {  /* set flags for all equivalenced vars */
  1936.       symtab *equiv=symt;
  1937.       do{
  1938.  if(! equiv->set_flag) {
  1939.     equiv->used_before_set = TRUE;
  1940.  }
  1941.  equiv->used_flag = TRUE;
  1942.  equiv = equiv->equiv_link;
  1943.       } while(equiv != symt);
  1944.     }
  1945.  
  1946. }/*use_variable*/
  1947.  
  1948.  
  1949. /*  End of symtab.c */
  1950.  
  1951. /*
  1952.  
  1953.  II. Hash
  1954.  
  1955. */
  1956.  
  1957. /*    hash.c:
  1958.   performs a hash function
  1959.  
  1960. This was formerly a separate file.
  1961.  
  1962. */
  1963.  
  1964. extern int sixclash; /* flag to check clashes in 1st 6 chars of name */
  1965.  
  1966. unsigned long
  1967. hash(s)
  1968.     char *s;
  1969. {
  1970.     unsigned long sum = 0, wd;
  1971.     int i = 0,j;
  1972.  
  1973.     int n = strlen(s);
  1974.     if(sixclash && n > 6) n = 6;
  1975.  
  1976.     while (i < n) {
  1977.          wd = 0;
  1978.          for(j=1; j <= sizeof(long) && i < n; i++,j++) {
  1979.             wd += (unsigned long)(s[i] & 0xff) << (sizeof(long) - j) * 8;}
  1980.  
  1981.  sum ^= wd;}
  1982.     return sum;
  1983. }
  1984.  
  1985.   /* Same as hash() but always uses full length of keyword.
  1986.      To keep the keyword table clash-free on any machine,
  1987.      packs only 4 bytes per word even if long is bigger */
  1988. unsigned long
  1989. kwd_hash(s)
  1990.     char *s;
  1991. {
  1992.     unsigned long sum = 0, wd;
  1993.     int i = 0,j;
  1994.  
  1995.     int n = strlen(s);
  1996.  
  1997.     while (i < n) {
  1998.          wd = 0;
  1999.          for(j=1; j <= 4 && i < n; i++,j++) {
  2000.             wd += (unsigned long)(s[i] & 0xff) << (4 - j) * 8;}
  2001.  
  2002.  sum ^= wd;}
  2003.     return sum;
  2004. }
  2005.  
  2006.  
  2007.  
  2008. /*    rehash.c
  2009.         performs a rehash for resolving clashes.
  2010. */
  2011.  
  2012. #ifdef COUNT_REHASHES
  2013. unsigned long rehash_count=0;
  2014. #endif
  2015.  
  2016. unsigned long
  2017. rehash(hnum)
  2018.     unsigned long hnum;
  2019. {
  2020. #ifdef COUNT_REHASHES
  2021.     rehash_count++;
  2022. #endif
  2023.     return hnum+1;
  2024. }
  2025.  
  2026.  
  2027. /*  End of hash */
  2028.  
  2029.  
  2030. /*
  2031.  
  2032. III. Intrins
  2033.  
  2034. */
  2035.  
  2036. /* intrinsic.c:
  2037.  
  2038.  Handles datatyping of intrinsic functions.
  2039. */
  2040.  
  2041.  
  2042.  /* File intrinsic.h contains information from Table 5, pp. 15-22
  2043.     to 15-25 of the standard.  Note: num_args == -1 means 1 or 2 args,
  2044.     num_args == -2 means 2 or more args.  Value of arg_type is the OR
  2045.     of all allowable types (I, R, etc. as defined above).  Value of
  2046.     result_type is type returned by function (type_INTEGER, etc.).
  2047.     If result_type is type_GENERIC, function type is same as arg type.
  2048.  */
  2049.  
  2050.  
  2051. IntrinsInfo intrinsic[]={
  2052. #include "intrins.h"
  2053. };
  2054.  
  2055. #define NUM_INTRINSICS (sizeof(intrinsic)/sizeof(intrinsic[0]))
  2056.  
  2057. #define EMPTY 255
  2058.  
  2059. unsigned char intrins_hashtab[INTRINS_HASHSZ];
  2060.  
  2061. /*    init_intrins_hashtab:
  2062.                  Initializes the intrinsic hash table by clearing it to EMPTY
  2063.                  and then hashes all the intrinsic names into the table.
  2064. */
  2065.  
  2066. unsigned long
  2067. init_intrins_hashtab()
  2068. {
  2069.     unsigned i,h;
  2070.     unsigned long hnum;
  2071.     unsigned long numclashes=0;
  2072.  
  2073.     for(h=0;h<INTRINS_HASHSZ;h++) {
  2074.            intrins_hashtab[h] = EMPTY;
  2075.     }
  2076.     for(i=0; i < NUM_INTRINSICS; i++) {
  2077.     hnum = kwd_hash(intrinsic[i].name);
  2078.     while(h=hnum%INTRINS_HASHSZ, intrins_hashtab[h] != EMPTY) {
  2079.   hnum = rehash(hnum);
  2080.   numclashes++;
  2081.     }
  2082.     intrins_hashtab[h] = i;
  2083.     }
  2084.     return numclashes;
  2085. }
  2086.  
  2087.  /* Function to look up an intrinsic function name in table.
  2088.     If found, returns ptr to table entry, otherwise NULL.
  2089.  */
  2090. PRIVATE IntrinsInfo *
  2091. find_intrinsic(s)
  2092.  char *s;   /* given name */
  2093. {
  2094.  unsigned i, h;
  2095.  unsigned long hnum;
  2096.  
  2097.  hnum = kwd_hash(s);
  2098.  while( h=hnum%INTRINS_HASHSZ, (i=intrins_hashtab[h]) != EMPTY &&
  2099.   strcmp(s,intrinsic[i].name) != 0) {
  2100.    hnum = rehash(hnum);
  2101.  }
  2102.  
  2103.  if(i != EMPTY) {
  2104.      return &intrinsic[i];
  2105.  }
  2106.  else
  2107.      return (IntrinsInfo *)NULL;
  2108. }
  2109.  
  2110.  /* find_io_keyword looks up an i/o keyword in io_keywords
  2111.     table and returns its index.  Uses simple linear search
  2112.     since not worth hash overhead.  If not found, returns
  2113.     index of last element of list, which is special. */
  2114. PRIVATE int
  2115. find_io_keyword(s)
  2116.      char *s;   /* given name */
  2117. {
  2118.     int i;
  2119.     for(i=0; io_keywords[i].name != NULL; i++) {
  2120.  if(strcmp(io_keywords[i].name, s) == 0) {
  2121.      break;
  2122.  }
  2123.     }
  2124.     return i;
  2125. }
  2126.  
  2127.